package game;

import java.awt.Point;
import java.util.*;

import org.w3c.dom.Element;

/**
 * <p>A class representing a {@code Rook} from a game of chess</p>
 * 
 * @author Nicole Waldrum
 * @date Janaury 30, 2010
 * @version 1.0
 *
 * @author Mark Kelly
 * @date February 19, 2010
 * @version 2.0
 * 
 * @author Trevor Bastian
 * @date February 20, 2010
 * @version 2.0
 * 
 * @author Mark Kelly
 * @date March 6, 2010
 * @version 3.0
 */
public class Rook extends Piece {
	// add this Piece subclass to the list of Piece types
	static {
		Piece.addPieceType(Rook.class);
	}
	
	private static final String DEFAULT_LETTER = "R";
    /**
     * <p>Creates a new instance of {@code Rook}</p>
     * 
     * @param name the name of this {@code Rook}
     * @param position the initial position of this {@code Rook}
     * @param team the {@code Team} that this {@code Rook} is on
     */
    public Rook(String name, Point position, Team team) {
        super(name, position, team);
    }
    
    /**
     * <p>Creates a new instance of {@code Rook}</p>
     * 
     * @param position the initial position of this {@code Rook}
     * @param team the {@code Team} that this {@code Rook} is on
     */
    public Rook(Point position, Team team) {
    	super(position, team);
    }
  
    public Rook (Element rook) {
    	super (rook);
    }
    
    /**
     * {@inheritDoc}
     */
    public String toLetter() {
    	return DEFAULT_LETTER;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Point> checkMovePattern(Point dest) {
        List<Point> ret = new ArrayList<Point>();
        Point p = this.getLocation();
        int dx = (int) ( dest . getX () - p . getX ());
        int dy = (int) ( dest . getY () - p . getY ());
        int i = ( Math . abs ( dx ) > Math . abs ( dy ) ) ? Math . abs ( dx ) : Math . abs ( dy );
        
        if ( dx != 0 )
            dx /= Math . abs ( dx );    // give dx a magnitude of 1
        if ( dy != 0 )
            dy /= Math . abs ( dy );    // give dy a magnitude of 1
        
        for ( int j = 0; j < i - 1; j++ ) {
            p = new Point ((((int) p . getX ()) + dx ), (((int) p . getY ()) + dy ));
            ret . add( p );
        }
        
        return ret;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isValidMove(Piece piece, Point dest) {
        Point start = piece.getLocation();
        if ( (int)( start . getX () - dest . getX ()) == 0 || (int)( start . getY () - dest . getY ()) == 0 )
            return super.isValidMove(piece, dest);

        return false;
    }
}
